// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'lianxi-h7yca'
})
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const { type } = event;
  if(type === 1){//首页获取用户信息
    const { openid, page, size } = event;
    const pageSize = Number((page-1)) * size;
    const res = await  cloud.database().collection('userList').skip(pageSize).limit(size).get();
    console.log('111',res)
    // const list = res.data.filter( item=> item.openid === openid )
    return{
      status: 'S',
      data: res.data,
      code: 200,
      message: '加载成功'
    }
  }else if(type === 2){//发现页面的上传图片及视频的文件id到数据库中
    const { openid, fileId, name, time } = event;
    console.log('文件id', fileId)
    const fileName = name === 'image' ? 'fileList' : 'videoList'
    const fileVal = await  cloud.database().collection(fileName).add({
      data:{
        openid,
        fileId,
        time
      }
    });
    console.log('返回结果', fileVal);
    if(fileVal.errMsg === "collection.add:ok"){
      return {
        code: 200,
        status: 'S',
        message: '上传成功'
      }
    }else{
      return {
        code: 2000,
        status: 'S',
        message: '上传失败'
      }
    }
   
  }else if(type ===3 ){//这是查看相册的页面进行调用的
    const { openid, page, size } = event;
    const pageSize = Number((page-1)) * size;
    const imgList = await  cloud.database().collection('fileList').where({
      openid
    }).skip(pageSize).limit(size).get();

    console.log('获取数据库中的图片数据', pageSize,size,imgList);
    const data = imgList.data.filter(item=> item.openid === openid );
    return {
      code: 200,
      status: 'S',
      message: '请求成功',
      data
    }
  }else if(type === 4){//删除图片的数据
    const { openid, fileId } = event;
    const removeImg = await  cloud.database().collection('fileList').where({
      openid,
      fileId
    }).remove();
    console.log('删除数据库中的图片',removeImg)
    if(removeImg.stats.removed > 0){
      return {
        code: 200,
        message: '删除成功',
        status: 'S'
      }
    }else{
      return {
        code: 2000,
        message: '删除失败',
        status: 'U'
      }
    }
  }else if(type === 5){//获取视频的数据
    const { openid, page, size } = event;
    const pageSize = Number((page-1)) * size;
    const videoList = await  cloud.database().collection('videoList').where({
      openid
    }).skip(pageSize).limit(size).get();

    console.log('获取数据库中的视频数据', videoList);
    const data = videoList.data.filter(item=> item.openid === openid );
    console.log('data==================',data)
    return {
      code: 200,
      status: 'S',
      message: '请求成功',
      data
    }
  }else if( type === 6){//写作的页面
    const { openid, introduce, title, logo,  detail, time } = event;
    try{
      const addList = await  cloud.database().collection('userList').add({
        data:{
          openid, 
          introduce,
          title, 
          logo, 
          detail,
          time
        }
      });
      console.log('111',addList)
      return{
        status: 'S',
        data: addList,
        code: 200,
        message: '加载成功'
      }
    }catch(e){
      console.log('写作页面提交失败',e)
      return{
        status: 'N',
        data: [],
        code: 20000,
        message: '加载失败'
      }
    }

  }else if( type === 7){//首页的详情页面，进行删除文章
    const { openid, id } = event;
   try{
    const removeList = await  cloud.database().collection('userList').where({
      openid,
      _id: id
    }).remove();
    console.log('详情页面的文章删除数据============',removeList)
    if(removeList.stats.removed > 0){
      return {
        code: 200,
        message: '删除成功',
        status: 'S'
      }
    }else{
      return {
        code: 20000,
        message: '删除失败',
        status: 'N'
      }
    }
   }catch(e){
     console.log('详情页面删除文章报错了=======',e)
     return {
       status: 'N',
       code: 20000,
       message: '删除失败'
     }
   }
  }else if( type === 8 ){//这是详情页的编辑跳转到编辑页面进行更新的操作
    const { openid, introduce, title, logo,  detail, time, id } = event;
    try{
      const updateList = await  cloud.database().collection('userList').where({
        openid,
        _id: id
      }).update({
        data:{
          openid, 
          introduce,
          title, 
          logo, 
          detail,
          time
        }
      });
      console.log('111',updateList)
      return{
        status: 'S',
        data: updateList,
        code: 200,
        message: '加载成功'
      }
    }catch(e){
      console.log('写作页面提交失败',e)
      return{
        status: 'N',
        data: [],
        code: 20000,
        message: '加载失败'
      }
    }
  }else if( type === 9){//制作简历，将用户填写的信息存存储到数据库中
    const { openid, list } = event;
    try{
      const resumeList = await  cloud.database().collection('resumeList').add({
        data:{
          openid,
          list
        }
      })
      console.log('111',resumeList)
      return{
        status: 'S',
        data: resumeList,
        code: 200,
        message: '制作简历成功'
      }
    }catch(e){
      console.log('制作简历失败',e)
      return{
        status: 'N',
        data: [],
        code: 20000,
        message: '制作简历失败'
      }
    }
  }else if( type === 10){//查询数据库中的用户创建的简历
    const { openid } = event;
    try{
      const getResumeList = await  cloud.database().collection('resumeList').get();
      console.log('111',getResumeList)
      const data = getResumeList.data.filter(item=>item.openid === openid)
      return{
        status: 'S',
        data,
        code: 200,
        message: '获取简历数据成功'
      }
    }catch(e){
      console.log('制作简历失败',e)
      return{
        status: 'N',
        data: [],
        code: 20000,
        message: '获取简历数据失败'
      }
    }
  }else if( type === 11){//删除简历
    const { openid, _id } = event;
    try{
      const removeResumeList = await  cloud.database().collection('resumeList').where({
        openid,
        _id
      }).remove()
      console.log('111',removeResumeList)
  
      return{
        status: 'S',
        data: removeResumeList,
        code: 200,
        message: '删除简历成功'
      }
    }catch(e){
      console.log('删除简历失败',e)
      return{
        status: 'N',
        data: [],
        code: 20000,
        message: '删除简历失败'
      }
    }
  }else if( type === 12){//设置修改用户的头像及用户名
    const { openid, data } = event;
    try{
      const userInfo = await cloud.database().collection('userInfo').where({
        openid
      }).get();
      console.log(userInfo)
      if(userInfo.data.length > 0){//有的话就进行更新
        const updateList = await  cloud.database().collection('userInfo').where({
          openid
        }).update({
          data:{
            data
          }
        })
        console.log('111',updateList)
        return{
          status: 'S',
          data: updateList,
          code: 200,
          message: '修改成功'
        }
      }else{//否则就是没有，进行添加
        const wrtiteList = await  cloud.database().collection('userInfo').add({
          data:{
            openid,
            data
          }
        })
        console.log('111',wrtiteList)
        return{
          status: 'S',
          data: wrtiteList,
          code: 200,
          message: '添加成功'
        }
      }
     
  
      
    }catch(e){
      console.log('修改失败',e)
      return{
        status: 'N',
        data: [],
        code: 20000,
        message: '修改失败'
      }
    }
  }else if( type === 13){//修改密码
    const { openid, _id, password } = event;
    try{
      const updatePassword = await cloud.database().collection('sign').where({
        openid,
        _id
      }).update({
        data:{
          password,
          password1: password
        }
      })
      console.log('密码修改成功',updatePassword)
      return{
        code: 200,
        status: 'S',
        message: '密码修改成功',
        data: updatePassword
      }
    }catch(e){
      console.log('密码修改失败',e)
      return{
        code: 20000,
        status: 'N',
        message: '密码修改失败',
        data: []
      }
    }

  }else if( type === 14){//获取用户的头像及用户名
    const { openid } = event;
    try{
      const getUser = await cloud.database().collection('userInfo').where({
        openid
      }).get()
      console.log('获取用户头像成功',getUser)
      return{
        code: 200,
        status: 'S',
        message: '获取用户信息成功',
        data: getUser
      }
    }catch(e){
      console.log('获取用户信息失败',e)
      return{
        code: 20000,
        status: 'N',
        message: '获取用户信息失败',
        data: []
      }
    }
  }




}